﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace UIShare
{
    public sealed class ControlNavigator
    {
        /// <summary>
        /// 需要納入管理的控件
        /// </summary>
        public Control[] Controls { get; private set; }

        /// <summary>
        /// 控件導航管理
        /// </summary>
        /// <param name="controls">需要納入管理的控件,參數的順序決定了導航的順序,必須小心!</param>
        public ControlNavigator(params Control[] controls)
        {
            Controls = controls;

            InitControls();
        }

        private void InitControls()
        {
            for (int index = 0; index < Controls.Length; index++)
            {
                Control ctrl = Controls[index];
                ctrl.TabIndex = index;
            }
        }

        /// <summary>
        /// 導航到下一個控件
        /// </summary>
        public void Next()
        {
            Next(null);
        }

        /// <summary>
        /// 導航到下一個控件
        /// </summary>
        /// <param name="action">焦點進入下一個控件後需要執行的函數</param>
        public void Next(Action<Control> action)
        {
            int currentFocusedIndex = GetFocusedControlIndex();

            int index = 0;

            if (currentFocusedIndex != -1 && currentFocusedIndex + 1 != Controls.Length)
            {
                index = currentFocusedIndex + 1;
            }

            Controls[index].Focus();

            if (action != null)
            {
                action.Invoke(Controls[index]);
            }
        }

        /// <summary>
        /// 導航回上一個控件
        /// </summary>
        public void Previous()
        {
            Previous(null);
        }

        /// <summary>
        /// 導航回上一個控件
        /// </summary>
        /// <param name="action">焦點進入上一個後需要執行的函數</param>
        public void Previous(Action<Control> action)
        {
            int currentFocusedIndex = GetFocusedControlIndex();

            int index = Controls.Length - 1;

            if (currentFocusedIndex != -1 && currentFocusedIndex - 1 >= 0)
            {
                index = currentFocusedIndex - 1;
            }

            Controls[index].Focus();

            if (action != null)
            {
                action.Invoke(Controls[index]);
            }
        }

        /// <summary>
        /// 取得當前焦點所在的控件在管理控件裡面的index
        /// </summary>
        /// <returns></returns>
        private int GetFocusedControlIndex()
        {
            for (int index = 0; index < Controls.Length; index++)
            {
                if (Controls[index].Focused)
                {
                    return index;
                }
            }

            return -1;
        }

        /// <summary>
        /// 將焦點移入某個控件
        /// </summary>
        /// <param name="control"></param>
        public void FocusTo(Control control)
        {
            control.Focus();
        }

        /// <summary>
        /// 將焦點移入某個控件
        /// </summary>
        /// <param name="index"></param>
        public void FocusTo(int index)
        {
            if (index < 0 || index >= Controls.Length)
            {
                return;
            }

            Controls[index].Focus();
        }
    }
}
